<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>simplepool.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_structs_simplepool.js.html">simplepool.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2007 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Datastructure: Pool.
<a name="line17"></a> *
<a name="line18"></a> *
<a name="line19"></a> * A generic class for handling pools of objects that is more efficient than
<a name="line20"></a> * goog.structs.Pool because it doesn&#39;t maintain a list of objects that are in
<a name="line21"></a> * use. See constructor comment.
<a name="line22"></a> */
<a name="line23"></a>
<a name="line24"></a>
<a name="line25"></a>goog.provide(&#39;goog.structs.SimplePool&#39;);
<a name="line26"></a>
<a name="line27"></a>goog.require(&#39;goog.Disposable&#39;);
<a name="line28"></a>
<a name="line29"></a>
<a name="line30"></a>
<a name="line31"></a>/**
<a name="line32"></a> * A generic pool class. Simpler and more efficient than goog.structs.Pool
<a name="line33"></a> * because it doesn&#39;t maintain a list of objects that are in use. This class
<a name="line34"></a> * has constant overhead and doesn&#39;t create any additional objects as part of
<a name="line35"></a> * the pool management after construction time.
<a name="line36"></a> *
<a name="line37"></a> * IMPORTANT: If the objects being pooled are arrays or maps that can have
<a name="line38"></a> * unlimited number of properties, they need to be cleaned before being
<a name="line39"></a> * returned to the pool.
<a name="line40"></a> *
<a name="line41"></a> * Also note that {@see goog.object.clean} actually allocates an array to clean
<a name="line42"></a> * the object passed to it, so simply using this function would defy the
<a name="line43"></a> * purpose of using the pool.
<a name="line44"></a> *
<a name="line45"></a> * @param {number} initialCount Initial number of objects to populate the free
<a name="line46"></a> *     pool at construction time.
<a name="line47"></a> * @param {number} maxCount Maximum number of objects to keep in the free pool.
<a name="line48"></a> * @constructor
<a name="line49"></a> * @extends {goog.Disposable}
<a name="line50"></a> * @template T
<a name="line51"></a> */
<a name="line52"></a>goog.structs.SimplePool = function(initialCount, maxCount) {
<a name="line53"></a>  goog.Disposable.call(this);
<a name="line54"></a>
<a name="line55"></a>  /**
<a name="line56"></a>   * Function for overriding createObject. The avoids a common case requiring
<a name="line57"></a>   * subclassing this class.
<a name="line58"></a>   * @private {Function}
<a name="line59"></a>   */
<a name="line60"></a>  this.createObjectFn_ = null;
<a name="line61"></a>
<a name="line62"></a>  /**
<a name="line63"></a>   * Function for overriding disposeObject. The avoids a common case requiring
<a name="line64"></a>   * subclassing this class.
<a name="line65"></a>   * @private {Function}
<a name="line66"></a>   */
<a name="line67"></a>  this.disposeObjectFn_ = null;
<a name="line68"></a>
<a name="line69"></a>  /**
<a name="line70"></a>   * Maximum number of objects allowed
<a name="line71"></a>   * @private {number}
<a name="line72"></a>   */
<a name="line73"></a>  this.maxCount_ = maxCount;
<a name="line74"></a>
<a name="line75"></a>  /**
<a name="line76"></a>   * Queue used to store objects that are currently in the pool and available
<a name="line77"></a>   * to be used.
<a name="line78"></a>   * @private {Array.&lt;T&gt;}
<a name="line79"></a>   */
<a name="line80"></a>  this.freeQueue_ = [];
<a name="line81"></a>
<a name="line82"></a>  this.createInitial_(initialCount);
<a name="line83"></a>};
<a name="line84"></a>goog.inherits(goog.structs.SimplePool, goog.Disposable);
<a name="line85"></a>
<a name="line86"></a>
<a name="line87"></a>/**
<a name="line88"></a> * Sets the {@code createObject} function which is used for creating a new
<a name="line89"></a> * object in the pool.
<a name="line90"></a> * @param {Function} createObjectFn Create object function which returns the
<a name="line91"></a> *     newly created object.
<a name="line92"></a> */
<a name="line93"></a>goog.structs.SimplePool.prototype.setCreateObjectFn = function(createObjectFn) {
<a name="line94"></a>  this.createObjectFn_ = createObjectFn;
<a name="line95"></a>};
<a name="line96"></a>
<a name="line97"></a>
<a name="line98"></a>/**
<a name="line99"></a> * Sets the {@code disposeObject} function which is used for disposing of an
<a name="line100"></a> * object in the pool.
<a name="line101"></a> * @param {Function} disposeObjectFn Dispose object function which takes the
<a name="line102"></a> *     object to dispose as a parameter.
<a name="line103"></a> */
<a name="line104"></a>goog.structs.SimplePool.prototype.setDisposeObjectFn = function(
<a name="line105"></a>    disposeObjectFn) {
<a name="line106"></a>  this.disposeObjectFn_ = disposeObjectFn;
<a name="line107"></a>};
<a name="line108"></a>
<a name="line109"></a>
<a name="line110"></a>/**
<a name="line111"></a> * Gets an unused object from the the pool, if there is one available,
<a name="line112"></a> * otherwise creates a new one.
<a name="line113"></a> * @return {T} An object from the pool or a new one if necessary.
<a name="line114"></a> */
<a name="line115"></a>goog.structs.SimplePool.prototype.getObject = function() {
<a name="line116"></a>  if (this.freeQueue_.length) {
<a name="line117"></a>    return this.freeQueue_.pop();
<a name="line118"></a>  }
<a name="line119"></a>  return this.createObject();
<a name="line120"></a>};
<a name="line121"></a>
<a name="line122"></a>
<a name="line123"></a>/**
<a name="line124"></a> * Returns an object to the pool so that it can be reused. If the pool is
<a name="line125"></a> * already full, the object is disposed instead.
<a name="line126"></a> * @param {T} obj The object to release.
<a name="line127"></a> */
<a name="line128"></a>goog.structs.SimplePool.prototype.releaseObject = function(obj) {
<a name="line129"></a>  if (this.freeQueue_.length &lt; this.maxCount_) {
<a name="line130"></a>    this.freeQueue_.push(obj);
<a name="line131"></a>  } else {
<a name="line132"></a>    this.disposeObject(obj);
<a name="line133"></a>  }
<a name="line134"></a>};
<a name="line135"></a>
<a name="line136"></a>
<a name="line137"></a>/**
<a name="line138"></a> * Populates the pool with initialCount objects.
<a name="line139"></a> * @param {number} initialCount The number of objects to add to the pool.
<a name="line140"></a> * @private
<a name="line141"></a> */
<a name="line142"></a>goog.structs.SimplePool.prototype.createInitial_ = function(initialCount) {
<a name="line143"></a>  if (initialCount &gt; this.maxCount_) {
<a name="line144"></a>    throw Error(&#39;[goog.structs.SimplePool] Initial cannot be greater than max&#39;);
<a name="line145"></a>  }
<a name="line146"></a>  for (var i = 0; i &lt; initialCount; i++) {
<a name="line147"></a>    this.freeQueue_.push(this.createObject());
<a name="line148"></a>  }
<a name="line149"></a>};
<a name="line150"></a>
<a name="line151"></a>
<a name="line152"></a>/**
<a name="line153"></a> * Should be overridden by sub-classes to return an instance of the object type
<a name="line154"></a> * that is expected in the pool.
<a name="line155"></a> * @return {T} The created object.
<a name="line156"></a> */
<a name="line157"></a>goog.structs.SimplePool.prototype.createObject = function() {
<a name="line158"></a>  if (this.createObjectFn_) {
<a name="line159"></a>    return this.createObjectFn_();
<a name="line160"></a>  } else {
<a name="line161"></a>    return {};
<a name="line162"></a>  }
<a name="line163"></a>};
<a name="line164"></a>
<a name="line165"></a>
<a name="line166"></a>/**
<a name="line167"></a> * Should be overrideen to dispose of an object. Default implementation is to
<a name="line168"></a> * remove all of the object&#39;s members, which should render it useless. Calls the
<a name="line169"></a> *  object&#39;s dispose method, if available.
<a name="line170"></a> * @param {T} obj The object to dispose.
<a name="line171"></a> */
<a name="line172"></a>goog.structs.SimplePool.prototype.disposeObject = function(obj) {
<a name="line173"></a>  if (this.disposeObjectFn_) {
<a name="line174"></a>    this.disposeObjectFn_(obj);
<a name="line175"></a>  } else if (goog.isObject(obj)) {
<a name="line176"></a>    if (goog.isFunction(obj.dispose)) {
<a name="line177"></a>      obj.dispose();
<a name="line178"></a>    } else {
<a name="line179"></a>      for (var i in obj) {
<a name="line180"></a>        delete obj[i];
<a name="line181"></a>      }
<a name="line182"></a>    }
<a name="line183"></a>  }
<a name="line184"></a>};
<a name="line185"></a>
<a name="line186"></a>
<a name="line187"></a>/**
<a name="line188"></a> * Disposes of the pool and all objects currently held in the pool.
<a name="line189"></a> * @override
<a name="line190"></a> * @protected
<a name="line191"></a> */
<a name="line192"></a>goog.structs.SimplePool.prototype.disposeInternal = function() {
<a name="line193"></a>  goog.structs.SimplePool.superClass_.disposeInternal.call(this);
<a name="line194"></a>  // Call disposeObject on each object held by the pool.
<a name="line195"></a>  var freeQueue = this.freeQueue_;
<a name="line196"></a>  while (freeQueue.length) {
<a name="line197"></a>    this.disposeObject(freeQueue.pop());
<a name="line198"></a>  }
<a name="line199"></a>  delete this.freeQueue_;
<a name="line200"></a>};
</pre>


</body>
</html>
